package Com.Hupochuan.Uil.Util;

import java.sql.Timestamp;
import java.util.List;

import Com.Hupochuan.Dal.Tab.ActorTab;
import Com.Hupochuan.Dal.Tab.AreaSortTab;
import Com.Hupochuan.Dal.Tab.DirectorTab;
import Com.Hupochuan.Dal.Tab.FriendLinkTab;
import Com.Hupochuan.Dal.Tab.MovieTab;
import Com.Hupochuan.Dal.Tab.PageMovieListTab;
import Com.Hupochuan.Dal.Tab.PageMovieTab;
import Com.Hupochuan.Dal.Tab.PlayTab;
import Com.Hupochuan.Dal.Tab.TypeTab;
import Com.Hupochuan.Dal.Tab.YearTab;
import Com.Hupochuan.Vo.Actor;
import Com.Hupochuan.Vo.AreaSort;
import Com.Hupochuan.Vo.Director;
import Com.Hupochuan.Vo.FriendLink;
import Com.Hupochuan.Vo.Movie;
import Com.Hupochuan.Vo.PageMovie;
import Com.Hupochuan.Vo.PageMovieList;
import Com.Hupochuan.Vo.Play;
import Com.Hupochuan.Vo.Type;
import Com.Hupochuan.Vo.Year;

/*
 * 内存池
 */
public class MemoryPond {
	/*
	 * 单一实例
	 */
	private MemoryPond(){
		long createTimeMillis = System.currentTimeMillis();
		this.createTime = new Timestamp(createTimeMillis);
		
		//初始化 年份 地区分类 类型 列表
		this.yearList = YearTab.getObject().findYearList();
		this.areaSortList = AreaSortTab.getObject().findAreaList();
		this.typeList = TypeTab.getObject().findTypeList();
		this.friendLinkList = FriendLinkTab.getObject().findFriendLinkList();
	}
	private static MemoryPond memoryPond = new MemoryPond();
	public static MemoryPond getObject(){
		return memoryPond;
	}
	
	/*
	 * 重置内存池
	 */
	public static void reset(){
		memoryPond = new MemoryPond();
	}
	
	private Timestamp createTime;
	private List<AreaSort> areaSortList;
	private List<Type> typeList;
	private List<Year> yearList;
	private HashBuffer<String,List<Director>> directorListHash = new HashBuffer<String,List<Director>>(100);
	private HashBuffer<String,List<Actor>> actorListHash = new HashBuffer<String,List<Actor>>(500);
	private List<Movie> tswkList;
	private List<FriendLink> friendLinkList;
	private String indexHtml;
	private HashBuffer<String,PageMovieList> pageMovieListHash = new HashBuffer<String,PageMovieList>(1000);
	private HashBuffer<String,PageMovie> pageMovieHash = new HashBuffer<String,PageMovie>(1000);
	private HashBuffer<String,Play> playHash = new HashBuffer<String,Play>(1000);
	
	/*
	 * 获得创建时间
	 */
	public Timestamp getCreateTime() {
		return this.createTime;
	}
	
	/*
	 * 获得转换的时间字符串
	 */
	public String getCreateTimeString() {
		return TimeStringUtil.getObject().timeToTimeString(this.createTime);
	}
	
	/*
	 * 获得年份对象
	 */
	public Year getYear(long yearId){
		//遍历年份列表
		for(Year year : this.getYearList()){
			//找到对应的年份对象
			if(year.getId() == yearId){
				return year;
			}
		}
		return null;
	}
	
	/*
	 * 获得地区分类对象
	 */
	public AreaSort getAreaSort(long areaSortId){
		//遍历地区分类列表
		for(AreaSort areaSort : this.getAreaSortList()){
			//找到对应的地区分类对象
			if(areaSort.getId() == areaSortId){
				return areaSort;
			}
		}
		return null;
	}
	
	/*
	 * 获得类型对象
	 */
	public Type getType(long typeId){
		//遍历类型列表
		for(Type type : this.getTypeList()){
			//找到对应的类型对象
			if(type.getId() == typeId){
				return type;
			}
		}
		return null;
	}
	
	/*
	 * 获得年份列表
	 */
	public List<Year> getYearList() {
		return this.yearList;
	}
	
	/*
	 * 获得地区分类列表
	 */
	public List<AreaSort> getAreaSortList() {
		return this.areaSortList;
	}
	
	/*
	 * 获得类型列表
	 */
	public List<Type> getTypeList() {
		return this.typeList;
	}
	
	/*
	 * 获得导演列表
	 */
	public List<Director> getDirectorList(long yearId,long areaSortId,long typeId) {
		
		//获得年份对象
		Year year = this.getYear(yearId);
		
		//用于查找HashBuffer的关键字
		StringBuffer keyBuffer = new StringBuffer();
		keyBuffer.append(yearId);
		keyBuffer.append("_");
		keyBuffer.append(areaSortId);
		keyBuffer.append("_");
		keyBuffer.append(typeId);
		
		List<Director> directorList = this.directorListHash.get(keyBuffer.toString());
		
		//如果内存中没有那就去数据库里找 因为是列表对象不可能为NULL 那就在找到后直接保存到内存
		if(directorList == null){
			directorList = DirectorTab.getObject().findDirectorListTop10(year, areaSortId, typeId);
			this.directorListHash.put(keyBuffer.toString(), directorList);
		}
	
		return directorList;
	}
	
	/*
	 * 获得导演列表内存列表
	 */
	public List<HashElement<String,List<Director>>> getDirectorListHashList(){
		return this.directorListHash.getHashList();
	}
	
	/*
	 * 获得演员列表
	 */
	public List<Actor> getActorList(long yearId,long areaSortId,long typeId,long directorId) {

		//获得年份对象
		Year year = this.getYear(yearId);
		
		//用于查找HashBuffer的关键字
		StringBuffer keyBuffer = new StringBuffer();
		keyBuffer.append(yearId);
		keyBuffer.append("_");
		keyBuffer.append(areaSortId);
		keyBuffer.append("_");
		keyBuffer.append(typeId);
		keyBuffer.append("_");
		keyBuffer.append(directorId);
		
		List<Actor> actorList = this.actorListHash.get(keyBuffer.toString());
		
		//如果内存中没有那就去数据库里找 因为是列表对象不可能为NULL 那就在找到后直接保存到内存
		if(actorList == null){
			actorList = ActorTab.getObject().findActorListTop10(year, areaSortId, typeId, directorId);
			this.actorListHash.put(keyBuffer.toString(), actorList);
		}
		
		return actorList;
	}
	
	/*
	 * 获得演员列表内存列表
	 */
	public List<HashElement<String,List<Actor>>> getActorListHashList(){
		return this.actorListHash.getHashList();
	}
	
	/*
	 * 获得电影列表页面
	 */
	public PageMovieList getPageMovieList(long yearId,long areaSortId,long typeId,long directorId,long actorId,long pageNo){
		
		//用于查找HashBuffer的关键字
		StringBuffer keyBuffer = new StringBuffer();
		keyBuffer.append(yearId);
		keyBuffer.append("_");
		keyBuffer.append(areaSortId);
		keyBuffer.append("_");
		keyBuffer.append(typeId);
		keyBuffer.append("_");
		keyBuffer.append(directorId);
		keyBuffer.append("_");
		keyBuffer.append(actorId);
		keyBuffer.append("_");
		keyBuffer.append(pageNo);
		
		PageMovieList pageMovieList = this.pageMovieListHash.get(keyBuffer.toString());
		
		//如果内存中没有那就去数据库里找
		if(pageMovieList == null){
			pageMovieList = PageMovieListTab.getObject().findPageMovieList(yearId, areaSortId, typeId, directorId, actorId, pageNo);
			
			//如果找到页 并且 不需要更新 那就在保存到内存
			if(pageMovieList != null && pageMovieList.getIsUpdate() == 0){
				this.pageMovieListHash.put(keyBuffer.toString(), pageMovieList);
			}
		}
		
		return pageMovieList;
	}
	
	/*
	 * 获得电影列表页面内存列表
	 */
	public List<HashElement<String,PageMovieList>> getPageMovieListHashList(){
		return this.pageMovieListHash.getHashList();
	}
	
	/*
	 * 获得电影页面
	 */
	public PageMovie getPageMovie(long movieId){
		
		//用于查找HashBuffer的关键字
		StringBuffer keyBuffer = new StringBuffer();
		keyBuffer.append(movieId);
		
		PageMovie pageMovie = this.pageMovieHash.get(keyBuffer.toString());
		
		//如果内存中没有那就去数据库里找
		if(pageMovie == null){
			pageMovie = PageMovieTab.getObject().findPageMovie(movieId);
			
			//如果找到页 并且 不需要更新 那就在保存到内存
			if(pageMovie != null && pageMovie.getIsUpdate() == 0){
				this.pageMovieHash.put(keyBuffer.toString(), pageMovie);
			}
		}
		
		return pageMovie;
	}
	
	/*
	 * 获得电影页面内存列表
	 */
	public List<HashElement<String,PageMovie>> getPageMovieHashList(){
		return this.pageMovieHash.getHashList();
	}
	
	/*
	 * 获得播放
	 */
	public Play getPlay(long playId){
		
		//用于查找HashBuffer的关键字
		StringBuffer keyBuffer = new StringBuffer();
		keyBuffer.append(playId);
		
		Play play = this.playHash.get(keyBuffer.toString());
		
		//如果内存中没有那就去数据库里找
		if(play == null){
			play = PlayTab.getObject().findPlay(playId);
			
			//如果找到播放数据
			if(play != null){
				this.playHash.put(keyBuffer.toString(), play);
			}
		}
		
		return play;
	}
	
	/*
	 * 获得播放内存列表
	 */
	public List<HashElement<String,Play>> getPlayHashList(){
		return this.playHash.getHashList();
	}
	
	/*
	 * 获得本周最热列表
	 */
	public List<Movie> getTswkList() {
		if(this.tswkList == null){
			this.tswkList = MovieTab.getObject().findTswkList();
		}
		return this.tswkList;
	}

	/*
	 * 获得友情链接列表
	 */
	public List<FriendLink> getFriendLinkList() {
		return friendLinkList;
	}

	/*
	 * 获得首页静态HTML
	 */
	public String getIndexHtml() {
		return indexHtml;
	}

	/*
	 * 设置首页静态HTML
	 */
	public void setIndexHtml(String indexHtml) {
		this.indexHtml = indexHtml;
	}
}
